<?php defined('SYSPATH') OR die('No direct access allowed.');

class Hongshaw_Controller extends Template_Controller {
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_hongshaw_add2';

	public function index(){
		
		$this->template->content = new View('hongshaw/form_add');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->room_types=ORM::factory("room_type")->find_all();
		$session = Session::instance();
		$this->template->title = 'เพิ่มห้องเช่า';
	}

	public function form_add(){
		$this->template->content = new View('hongshaw/form_add');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->hongshaws=ORM::factory("hongshaw")->find_all();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->room_types=ORM::factory("room_type")->find_all();
		$session = Session::instance();
		$this->template->title = 'เพิ่มห้องเช่า';

	}
	
	public function insert(){


		if ($_POST && Captcha::valid($this->input->post('captcha_response'))){
			$files1 = Validation::factory($_FILES)
			->add_rules('image1', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
			$files2 = Validation::factory($_FILES)
			->add_rules('image2', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
			$files3 = Validation::factory($_FILES)
			->add_rules('image3', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
			$files4 = Validation::factory($_FILES)
			->add_rules('image4', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
			
			$fileTemp1="";
			$fileTempThumbnail1="";
			$fileTemp2="";
			$fileTempThumbnail2="";
			$fileTemp3="";
			$fileTempThumbnail3="";
			$fileTemp4="";
			$fileTempThumbnail4="";
			if ($files1->validate()){
				// Temporary file name
				$filename1 = upload::save('image1');
				// Resize, sharpen, and save the image
				Image::factory($filename1)
				->save(DOCROOT.'images_hongshaw/pictures/'.basename($filename1));
				// Resize, sharpen, and save the image
				Image::factory($filename1)
				->resize(150,150, Image::WIDTH)
				->save(DOCROOT.'images_hongshaw/pictures/thumbnails/'.basename($filename1));
	
				// File name 1
				$fileTemp1='images_hongshaw/pictures/'.basename($filename1);
				$fileTempThumbnail1='images_hongshaw/pictures/thumbnails/'.basename($filename1);
				// Remove the temporary file
				unlink($filename1);
			}
			if ($files2->validate()){
				// Temporary file name
				$filename2 = upload::save('image2');
				// Resize, sharpen, and save the image
				Image::factory($filename2)
				->save(DOCROOT.'images_hongshaw/pictures/'.basename($filename2));
				// Resize, sharpen, and save the image
				Image::factory($filename2)
				->resize(150, 150, Image::WIDTH)
				->save(DOCROOT.'images_hongshaw/pictures/thumbnails/'.basename($filename2));
	
				// File name 2
				$fileTemp2='images_hongshaw/pictures/'.basename($filename2);
				$fileTempThumbnail2='images_hongshaw/pictures/thumbnails/'.basename($filename2);
	
				// Remove the temporary file
				unlink($filename2);
			}
			if ($files3->validate()){
				// Temporary file name
				$filename3 = upload::save('image3');
				// Resize, sharpen, and save the image
				Image::factory($filename3)
				->save(DOCROOT.'images_hongshaw/pictures/'.basename($filename3));
				// Resize, sharpen, and save the image
				Image::factory($filename3)
				->resize(150, 150, Image::WIDTH)
				->save(DOCROOT.'images_hongshaw/pictures/thumbnails/'.basename($filename3));
	
				// File name 3
				$fileTemp3='images_hongshaw/pictures/'.basename($filename3);
				$fileTempThumbnail3='images_hongshaw/pictures/thumbnails/'.basename($filename3);
	
				// Remove the temporary file
				unlink($filename3);
			}
			if ($files4->validate()){
				// Temporary file name
				$filename4 = upload::save('image4');
				// Resize, sharpen, and save the image
				Image::factory($filename4)
				->save(DOCROOT.'images_hongshaw/pictures/'.basename($filename4));
				// Resize, sharpen, and save the image
				Image::factory($filename4)
				->resize(150, 150, Image::WIDTH)
				->save(DOCROOT.'images_hongshaw/pictures/thumbnails/'.basename($filename4));
	
				// File name 3
				$fileTemp4='images_hongshaw/pictures/'.basename($filename4);
				$fileTempThumbnail4='images_hongshaw/pictures/thumbnails/'.basename($filename4);
	
				// Remove the temporary file
				unlink($filename4);
			}
			//echo $fileTemp1;
			//echo $fileTemp2;
			//echo $fileTemp3;
			
			/**
			 *  
			 */
			$hongshaw = new Hongshaw_Model();
			$hongshaw->own_name=$this->input->post("own_name");
			$hongshaw->own_tel =$this->input->post("own_tel");
			$hongshaw->own_email=$this->input->post("own_email");
			$hongshaw->hongshaw_name=$this->input->post("hongshaw_name");
			$hongshaw->hongshaw_min_price=$this->input->post("hongshaw_min_price");
			$hongshaw->hongshaw_max_price=$this->input->post("hongshaw_max_price");
			$hongshaw->hongshaw_prakan=$this->input->post("hongshaw_prakan");
			$hongshaw->hongshaw_klang=$this->input->post("hongshaw_klang");
			$hongshaw->hongshaw_water=$this->input->post("hongshaw_water");
			$hongshaw->hongshaw_elect=$this->input->post("hongshaw_elect");
			$hongshaw->hongshaw_ubc=$this->input->post("hongshaw_ubc");
			$hongshaw->hongshaw_tel=$this->input->post("hongshaw_tel");
			$hongshaw->hongshaw_furniture=$this->input->post("hongshaw_furniture");
			$hongshaw->hongshaw_air=$this->input->post("hongshaw_air");
			$hongshaw->hongshaw_bath=$this->input->post("hongshaw_internet");
			$hongshaw->hongshaw_internet=$this->input->post("name");
			$hongshaw->hongshaw_bts=$this->input->post("hongshaw_bts");
			$hongshaw->hongshaw_mrt=$this->input->post("hongshaw_mrt");
			$hongshaw->hongshaw_no=$this->input->post("hongshaw_no");
			$hongshaw->hongshaw_soi=$this->input->post("hongshaw_soi");
			$hongshaw->hongshaw_road=$this->input->post("hongshaw_road");
			$hongshaw->hongshaw_tumboon=$this->input->post("hongshaw_tumboon");
			$hongshaw->hongshaw_maung=$this->input->post("hongshaw_maung");
			$hongshaw->hongshaw_zipcode=$this->input->post("hongshaw_zipcode");
			$hongshaw->hongshaw_telphone=$this->input->post("hongshaw_telphone");
			$hongshaw->hongshaw_fax=$this->input->post("hongshaw_fax");
			$hongshaw->hongshaw_website=$this->input->post("hongshaw_website");
			$hongshaw->hongshaw_detail=$this->input->post("hongshaw_detail");
			$hongshaw->hongshaw_agency=$this->input->post("hongshaw_agency");
			$hongshaw->post_date=date("Y-m-d H:i:s");
			$hongshaw->hongshaw_interest_ads=$this->input->post("hongshaw_interest_ads");
			$hongshaw->thumbnail=$fileTempThumbnail1;
			$hongshaw->room_type_id=$this->input->post("room_type_id");
			$hongshaw->category_id=$this->input->post("category_id");
			$hongshaw->province_id=$this->input->post("province_id");
			$hongshaw->university_id=$this->input->post("university_id");
			$hongshaw->bkkzone_id=$this->input->post("bkkzone_id");
			$hongshaw->save();
			/* ******************************************  */
			if($fileTemp1!=""){
				$hongshawImage = new Hongshaw_Image_Model();
				$hongshawImage->description="";
				$hongshawImage->full_image=$fileTemp1;
				$hongshawImage->thumbnail=$fileTempThumbnail1;
				$hongshawImage->hongshaw_id=$hongshaw->id;
				$hongshawImage->save();
			}
			if($fileTemp2!=""){
				$hongshawImage = new Hongshaw_Image_Model();
				$hongshawImage->description="";
				$hongshawImage->full_image=$fileTemp2;
				$hongshawImage->thumbnail=$fileTempThumbnail2;
				$hongshawImage->hongshaw_id=$hongshaw->id;
				$hongshawImage->save();
			}
			if($fileTemp3!=""){
				$hongshawImage = new Hongshaw_Image_Model();
				$hongshawImage->description="";
				$hongshawImage->full_image=$fileTemp3;
				$hongshawImage->thumbnail=$fileTempThumbnail3;
				$hongshawImage->hongshaw_id=$hongshaw->id;
				$hongshawImage->save();
			}
		   if($fileTemp4!=""){
				$hongshawImage = new Hongshaw_Image_Model();
				$hongshawImage->description="";
				$hongshawImage->full_image=$fileTemp4;
				$hongshawImage->thumbnail=$fileTempThumbnail4;
				$hongshawImage->hongshaw_id=$hongshaw->id;
				$hongshawImage->save();
			}
			//$this->template->content = new View('hongshaw/form_add');
			url::redirect("hongshaw/hongshaw/index");
		}else{
			url::redirect("hongshaw/hongshaw/form_add");
		}
		
	}
	
	public function findDetail($id){
		
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('hongshaw_detail');
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->hongshaw =ORM::factory("hongshaw")->where("id",$id)->find();
		$hongshaw_id=$this->template->content->hongshaw->id;
		$this->template->content->hongshaw_images=ORM::factory("hongshaw_image")->where("hongshaw_id",$hongshaw_id)->find_all();
		$this->template->content->keyword='';
		$this->template->keyword="";
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	
	public function listResult(){
		$this->template->content = new View('hongshaw/listResult');
		$this->template->content->hongshaws=ORM::factory("hongshaw")->find_all();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->bkkzones=ORM::factory("bkkzone")->find_all();
		$this->template->content->universitys=ORM::factory("university")->find_all();
		$this->template->content->provinces=ORM::factory("province")->find_all();
		$this->template->content->room_types=ORM::factory("room_type")->find_all();
		$this->template->title = 'แสดงรายการ ห้องเช่า ทั้งหมด';
	}
}
?>
